<template>
    <div>
        <el-header>
            <el-page-header content="任务列表" @back="goBack"></el-page-header>
            <el-divider></el-divider>
        </el-header>

        <div style="margin-left: 10px">
            任务名：
            <el-input v-model="params.jobName" style="width: 200px;"></el-input>
            <el-button type="primary" @click="getPage" style="margin-left: 20px">查询</el-button>
            <el-button type="primary" @click="addJob" style="margin-left: 20px">新增任务</el-button>
        </div>

        <el-table :data="pageResult.records" border style="margin-top: 20px">
            <el-table-column label="任务名" prop="jobName"></el-table-column>
            <el-table-column label="任务组" prop="jobGroup"></el-table-column>
            <el-table-column label="任务描述" prop="description"></el-table-column>
            <el-table-column label="任务执行类" prop="jobClassName"></el-table-column>
            <el-table-column label="cron表达式" prop="cronExpression"></el-table-column>
            <el-table-column label="任务状态" prop="jobStatus">
                <template slot-scope="scope">
                    <span style="color: green;font-weight: 700" v-if="scope.row.jobStatus === '2'">正在运行</span>
                    <span style="color: red;font-weight: 700" v-if="scope.row.jobStatus === '1'">暂停中</span>
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-dropdown @command="handleCommand" trigger="click" :hide-on-click="true">
                        <el-button>
                            操作<i class="el-icon-arrow-down el-icon--right"></i>
                        </el-button>
                        <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item v-if="scope.row.jobStatus === '2'" :command="{operation:'pause',data:scope.row}">暂停</el-dropdown-item>
                            <el-dropdown-item v-if="scope.row.jobStatus === '1'" :command="{operation:'resume',data:scope.row}">继续</el-dropdown-item>
                            <el-dropdown-item :command="{operation:'delete',data:scope.row}">删除</el-dropdown-item>
                            <el-dropdown-item :command="{operation:'update',data:scope.row}">修改</el-dropdown-item>
                        </el-dropdown-menu>
                    </el-dropdown>
                </template>
            </el-table-column>
        </el-table>
        <el-divider></el-divider>
        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="pageResult.current"
                :page-sizes="[5, 10, 15, 20]"
                :page-size="pageResult.size"
                layout="total, sizes, prev, pager, next, jumper"
                :total="pageResult.total">
        </el-pagination>

        <el-dialog
                title="新增/修改任务"
                :visible.sync="dialogVisible"
                width="30%">
            <job-details-add-or-update ref="edit" @completed="handleCompleted"></job-details-add-or-update>
        </el-dialog>
    </div>
</template>

<script>
    import {JobDetailsService} from "../../../model/gs/JobDetailsService";

    const jobDetailsAddOrUpdate = () => import('./jobDetails-add-or-update.vue')

    const jobDetailsService = JobDetailsService.getInstance()

    export default {
        data() {
            return {
                dialogVisible: false,
                params: {
                    size: 5,
                    page: 1,
                    jobName: ''
                },
                pageResult: {}
            }
        },
        methods: {
            getPage() {
                jobDetailsService.pageQuery(this.params).then(({data}) => {
                    if (data.code === 0) {
                        this.pageResult = data.data
                    }
                })
            },
            addJob() {
                this.dialogVisible = true
                this.$nextTick(() =>{
                    this.$refs.edit.init(null)
                })
            },
            //size改变的方法
            handleSizeChange(size) {
                this.params.size = size
                this.getPage()
            },
            //当前页改变的方法
            handleCurrentChange(page) {
                this.params.page = page
                this.getPage()
            },
            handleCompleted() {
                this.dialogVisible = false
                this.getPage()
            },
            handleCommand(command) {
                console.log(command)
                switch (command.operation) {
                    case 'pause':
                        this.handlePause(command.data);
                        break;
                    case 'resume':
                        this.handleResume(command.data);
                        break;
                    case 'delete':
                        this.handleDelete(command.data);
                        break;
                    case 'update':
                        this.handleUpdate(command.data);
                        break;
                }
            },
            //暂停
            handlePause(data) {
                jobDetailsService.pause(data).then(({data}) => {
                    if(data.code === 0){
                        this.$message.success({
                            message: '暂停成功',
                            duration: 1000
                        })
                        this.getPage()
                    }
                })
            },
            //继续
            handleResume(data) {
                jobDetailsService.resume(data).then(({data}) => {
                    if(data.code === 0){
                        this.$message.success({
                            message: '任务继续',
                            duration: 1000
                        })
                        this.getPage()
                    }
                })
            },
            //继续
            handleDelete(data) {
                jobDetailsService.delete(data).then(({data}) => {
                    if(data.code === 0){
                        this.$message.success({
                            message: '删除成功',
                            duration: 1000
                        })
                        this.getPage()
                    }
                })
            },
            //更新任务
            handleUpdate(data){
                this.dialogVisible = true
                this.$nextTick(() => {
                    this.$refs.edit.init(data.jobName)
                })
            },
            goBack() {
                history.back()
            }
        },
        created() {
            this.getPage()
        },
        components: {
            jobDetailsAddOrUpdate
        }
    }
</script>

<style scoped>

</style>
